<template>
  <el-dialog :title="info.title" :visible.sync="info.isShow">
    <el-form :model="form">
      <el-form-item label="上级分类" :label-width="formLabelWidth">
        <el-select v-model="form.pid" placeholder="--请选择-">
          <el-option label="顶级分类" :value="0"></el-option>
          <!-- 遍历分类名称 -->
          <el-option
            v-for="item in cate"
            :key="item.id"
            :label="item.catename"
            :value="item.id"
          ></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="分类名称" :label-width="formLabelWidth">
        <el-input v-model="form.catename"></el-input>
      </el-form-item>
      <el-form-item label="图片" :label-width="formLabelWidth">
        <el-upload
          class="avatar-uploader"
          action="#"
          :show-file-list="false"
          :on-change="changeImg"
        >
          <img v-if="imageUrl" :src="imageUrl" class="avatar" />
          <i v-else class="el-icon-plus avatar-uploader-icon"></i>
        </el-upload>
      </el-form-item>
      <el-form-item label="状态" :label-width="formLabelWidth">
        <el-switch
          v-model="form.status"
          active-color="blue"
          inactive-color="grey"
          :active-value="1"
          :inactive-value="2"
        ></el-switch>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="confirm" v-if="info.isAdd"
        >确 定</el-button
      >
      <el-button type="primary" @click="update" v-else>修 改</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import { warningAlert, successAlert } from "../../../utils/alert";
import { cateAdd, cateOne } from "../../../request/api";
import { mapActions, mapGetters } from "vuex";
export default {
  props: ["info"],
  data() {
    return {
      form: {
        pid: 0, //顶级分类的pid
        catename: "",
        img: "",
        status: 1,
      },
      formLabelWidth: "120px",
      imageUrl: "",
    };
  },
  computed: {
    ...mapGetters({
      cate: "cate/cate",
    }),
  },
  methods: {
    ...mapActions({
      requestCate: "cate/cateListActions",
      cateUpdateActions: "cate/cateUpdateActions",
    }),
    changeImg(file) {
      // 1处理文件大小
      if (file.size > 2 * 1024 * 1024) {
        warningAlert("上传文件不能超过2M");
        return;
      }
      // 2.处理文件格式   图片: .jpg .png .jpeg .gif
      let ext = [".jpg", ".png", ".jpeg", ".gif"];
      let extName = file.name.slice(file.name.lastIndexOf("."));
      if (!ext.some((item) => item == extName)) {
        warningAlert("上传文件格式不正确");
        return;
      }

      // 3.生成本地url地址,赋值给imageUrl
      this.imageUrl = URL.createObjectURL(file.raw);
      // 4.将文件信息赋值给form.img
      this.form.img = file.raw;
    },
    cancel() {
      this.info.isShow = false;
      this.form = {
        pid: 0, //顶级分类的pid
        catename: "",
        img: "",
        status: 1,
      };
      this.imageUrl = "";
    },
    confirm() {
      cateAdd(this.form).then((res) => {
        if (res.code === 200) {
          successAlert(res.msg);
          this.cancel();
        }
      });
       this.requestCate();
    },
    goDetail(id) {
      cateOne({ id }).then((res) => {
        if (res.code === 200) {
          this.form = res.list;
          this.form.id=id;
          this.imageUrl = this.$pregImg + this.form.img;
        }
      });
    },
    update() {
      this.cateUpdateActions(this.form);
      this.cancel();
      this.requestCate();
    },
  },
  mounted() {
    this.requestCate();
   
  },
};
</script>
<style scoped>
/* >>> 穿透,   穿过scoped局部作用于进入element-ui的样式 */
.avatar-uploader >>> .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader >>> .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>